/**
 * 所有版权归 广西梧州 陈锦韬 所有
 *
 * @Title: InsertBatchSomeColumn
 * @Package com.rx.core.dbase
 * @Description: 批量插入处理函数
 * @author: 陈锦韬
 * @date: 2021/6/18
 * @version V1.0
 * @Copyright: 2021 陈锦韬  All rights reserved.
 */
package com.rx.core.dbase;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import lombok.NoArgsConstructor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * @author: CHENJINTAO170
 * @Description: 批量更新处理函数
 * @date: 2021/6/18
 */
@NoArgsConstructor
//@AllArgsConstructor
@SuppressWarnings("serial")
public class DeleteBatchSomeColumn extends AbstractMethod {


    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String sql = UpdateBatchSomeColumn.UPDATE_FORMAT;

        String setSql = getSetSql(tableInfo);
        String bidCondtionSql = UpdateBatchSomeColumn.UPDATE_BID_CONDITION;
        String verCondtionSql = UpdateBatchSomeColumn.UPDATE_VER_CONDITION;
        String sqlResult = String.format(sql, tableInfo.getTableName(), setSql,bidCondtionSql,verCondtionSql);

        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sqlResult, modelClass);
        return this.addUpdateMappedStatement(mapperClass, modelClass, "batchDeleteNoXml", sqlSource);
    }

    public String getSetSql(TableInfo tableInfo){
        StringBuilder valueSql = new StringBuilder();
        valueSql.append("<trim prefix=\"set\" suffixOverrides=\",\">");
        valueSql.append("<trim prefix=\"valid =case\" suffix=\"end,\">");

        valueSql.append("<foreach collection=\"list\" item=\"item\" index=\"index\">");
        valueSql.append("when bid=#{item.bid} and ver=#{item.ver} and valid='0' then '1' ");
        valueSql.append("</foreach>");

        valueSql.append("</trim>");
        valueSql.append("</trim>");
        return valueSql.toString();
    }

}
